package com.xtinno.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xtinno.mapper.RuleConditionMapper;
import com.xtinno.pojo.po.RuleConditionPO;
import com.xtinno.service.RuleConditionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 规则条件
 *
 * @author GuanLei
 * @time 2025/5/6 17:14
 */
@Service
public class RuleConditionServiceImpl extends ServiceImpl<RuleConditionMapper, RuleConditionPO> implements RuleConditionService {


    @Override
    public List<RuleConditionPO> getRuleConditionPOs(RuleConditionPO ruleConditionPO) {
        LambdaQueryWrapper<RuleConditionPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RuleConditionPO::getReasoningStage, ruleConditionPO.getReasoningStage());

        // 案件类型：等于 "-1" 或者 包含关键字（OR 条件）
        queryWrapper.and(wrapper -> wrapper
                .eq(RuleConditionPO::getCaseType, "-1")
                .or()
                .like(StringUtils.isNoneEmpty(ruleConditionPO.getCaseType()), RuleConditionPO::getCaseType, ruleConditionPO.getCaseType())
        );

        // 案由：等于 "-1" 或者 包含关键字（OR 条件）
        queryWrapper.and(wrapper -> wrapper
                .eq(RuleConditionPO::getCause, "-1")
                .or()
                .like(StringUtils.isNoneEmpty(ruleConditionPO.getCause()), RuleConditionPO::getCause, ruleConditionPO.getCause())
        );

        if (StringUtils.isEmpty(ruleConditionPO.getDocumentType())){
            queryWrapper.eq(RuleConditionPO::getDocumentType, "");
        }else {
            queryWrapper.like(RuleConditionPO::getDocumentType, ruleConditionPO.getDocumentType());
        }
        return this.list(queryWrapper);
    }

    @Override
    public Boolean saveRuleConditions(List<RuleConditionPO> ruleConditionPOS) {
        return this.saveBatch(ruleConditionPOS);
    }
}
